﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PSLib.CodeFormating.Formatters;
using System.Xml.Linq;

namespace LuaCodeStyler
{
	public partial class TemplateManagerForm : Form
	{
		public TemplateManagerForm()
		{
			InitializeComponent();
		}

		private void TemplateManagerForm_Load(object sender, EventArgs e)
		{
			UpdateUI();
		}

		private void UpdateUI()
		{
			quickInfoTextBox.Clear();
			listBox1.Items.Clear();
			foreach (var item in Templates.SettingsList)
				listBox1.Items.Add(item);

			listBox1.SelectedIndex = Templates.ActiveFormatIndex;
		}

		private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
		{
			FormatConfiguration s = listBox1.SelectedItem as FormatConfiguration;
			quickInfoTextBox.Clear();
			if (s != null)
			{
				AppendValue("Name","");
				AppendText(s.Name + "\n\n", Color.LightBlue, new Font("courier new", 8, FontStyle.Italic));
				AppendValue("Description", "");
				AppendText(s.Description + "\n", Color.LightBlue, new Font("courier new", 8, FontStyle.Italic));
			}
		}

		private void buttonAdd_Click(object sender, EventArgs e)
		{
			TemplateFormatEditForm f = new TemplateFormatEditForm();
			if (f.ShowDialog() == DialogResult.OK)
			{
				Templates.AddFormat(f.FormatSettings);
				UpdateUI();
			}
		}

		private void buttonEdit_Click(object sender, EventArgs e)
		{
			TemplateFormatEditForm f = new TemplateFormatEditForm();
			f.FormatSettings = Templates.GetFormat(listBox1.SelectedIndex);
			if (f.ShowDialog() == DialogResult.OK)
			{
				Templates.ReplaceFormat(listBox1.SelectedIndex,f.FormatSettings);
				UpdateUI();
			}
		}

		private void buttonRemove_Click(object sender, EventArgs e)
		{
			if (listBox1.SelectedIndex != -1)
			{
				Templates.RemoveFormat(listBox1.SelectedIndex);
				UpdateUI();
			}
		}

		private void buttonImport_Click(object sender, EventArgs e)
		{
			OpenFileDialog d = new OpenFileDialog();
			d.Filter = "*.xml|*.xml";
			if (d.ShowDialog() == DialogResult.OK)
			{
				FormatConfiguration fc = XMLOperations.Import(d.FileName);
				if (fc != null)
				{
					Templates.AddFormat(fc);
					UpdateUI();
				}
				else
				{
					MessageBox.Show("Cannot import format style. Check its signature","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
				}
			}
		}

		private void buttonExport_Click(object sender, EventArgs e)
		{
			FormatConfiguration fc = listBox1.SelectedItem as FormatConfiguration;
			if (fc == null) return;

			SaveFileDialog d = new SaveFileDialog();
			d.Filter = "*.xml|*.xml";
			d.DefaultExt = "xml";
			if (d.ShowDialog() == DialogResult.OK)
				XMLOperations.Export(d.FileName, fc);
		}

		private void AppendValue(string text, object value)
		{
			AppendText(string.Format("{0,-15}\t", text + ":"), Color.White, new Font("courier new", 8));
			AppendText(value.ToString() + "\n", Color.LightBlue, new Font("courier new", 8, FontStyle.Italic));
		}

		private void AppendText(string text)
		{
			AppendText(text, Color.Black, new Font("Verdana", 8));
		}

		private void AppendText(string text, Color color, Font font)
		{
			int startIndex = quickInfoTextBox.Text.Length;
			quickInfoTextBox.AppendText(text);
			quickInfoTextBox.Select(startIndex, text.Length);
			quickInfoTextBox.SelectionColor = color;
			quickInfoTextBox.SelectionFont = font;
			quickInfoTextBox.Select(startIndex + text.Length, 0);
		}
	}
}
